<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="warning" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange" highlight-current-row
			style="width: 100%;">
			<!--多选框-->
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="70" label="序号"  align="center">
			</el-table-column>
      <el-table-column prop="username" label="账号" show-overflow-tooltip  width="120" align="center">
			</el-table-column>
<!--      <el-table-column prop="password" label="密码" show-overflow-tooltip  width="120" align="center">-->
<!--			</el-table-column>-->
      <el-table-column prop="phone" label="电话"  align="center">
			</el-table-column>
      <el-table-column prop="email" label="邮箱" align="center">
			</el-table-column>
      <el-table-column prop="age" label="年龄" align="center">
			</el-table-column>
      <el-table-column prop="state" label="状态" align="center">
        <template scope="scope">
          <el-tag v-if="scope.row.state == 1" type="green">启用</el-tag>
          <el-tag v-else-if="scope.row.state == 0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="roles" label="角色" align="center">
        <template scope="scope">
          <el-tag v-for="role in scope.row.roles" :key="role" style="margin-right: 5px;">{{ role }}</el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column prop="logininfo_id" label="登录信息Id" show-overflow-tooltip align="center">-->
<!--      </el-table-column>-->
			<el-table-column label="操作"  align="center">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
				:page-sizes="[10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="totals" style="float:right;">
			</el-pagination>
		</el-col>

		<!--添加或修改界面-->
		<el-dialog title="保存用户" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="账号" prop="employeeName">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="saveForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="saveForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <template>
            <el-select v-model="saveForm.myRoles" clearable multiple placeholder="请选择">
                  <el-option v-for="item in options" :label="item.name" :value="item.id">
                  </el-option>
            </el-select>{{this.saveForm.myRoles}}
          </template>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio :label="0" >禁用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

import axios from "axios";
export default {
	data() {
		return {
      options:[],//保存所有角色信息
      //上传
      fileList: [],
      //显示所有父文章类型
      types:[],
			//1.分页查询相关数据
			totals: 0,//总记录条数
			currentPage: 1,//当前页,要传递到后台的
			pageSize: 10, //每页显示多少条
			tableDatas: [], //当前页数据
			listLoading: false,
			//2.高级查询相关数据
			keyword: '',
			//3.删除相关数据
			//4.批量删除相关数据
			sels: [], //存储多选记录
      //5.添加和修改相关数据
			saveFormVisible: false,
			saveForm: {
        id: null,
        username:'',
        phone:'',
        email:'',
        password:'',
        age:'',
        state:1,
        logininfo_id:'',
        roles:[],
			},
			//6.添加和修改校验规则-表单项校验
			saveFormRules: {
				// employeeName: [
				// 	{ required: true, message: '请输入文章名称', trigger: 'blur' }
				// ],
        // employeeContent: [
        //   { required: true, message: '请输入文章内容', trigger: 'blur' }
        // ]
			},
		}
	},
	methods: {
		//1.分页查询相关方法
		//1.1.获取列表数据
		getTableData() {
      var params = {
        keyword:this.keyword,
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }
      this.$http.post("/employee", params).then(res => {{
        this.totals = res.data.total;

        this.tableDatas = res.data.list;
        console.log(res.data.list)
      }})
		},
		//1.2.选择第几页时触发
		handleCurrentChange(curentPage) {
      this.currentPage = curentPage;
      this.getTableData()
    },
		//1.3.选择每页显示记录条数时触发
		handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getTableData()
    },

		//2.高级查询相关方法
		keywordQuery() {
      this.currentPage = 1;
			this.getTableData()
		},

		//3.删除相关方法
    handleDel(row) {
      console.log(row.id);
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        this.$http.delete("/employee/" + row.id).then(res => {
              if (res.data.success) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                });
                this.getTableData();
              } else {
                this.$message({
                  type: 'error',
                  message: '删除失败-500'
                });
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
		//4.批量删除相关方法
		//4.1.选项改变时触发
		handleSelectionChange(val) {
        this.sels = val;
		},
		//4.2.批量删除
		handlePatchDel() {
      var ids = this.sels.map(item =>item.id)
      console.log(ids)
      this.$confirm('此操作将永久选中数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.patch("/employee",ids).then(res =>{
          if(res.data.success) {
            this.$message({
              type: 'success',
              message: '删除成功!-200'
            });
            this.getTableData();
          } else {
            this.$message({
              type: 'error',
              message: '删除失败-500'
            });
          }
        }).catch(res => {
          this.$message({
            type: 'error',
            message: '网络繁忙,请稍后重试'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
		},
		//5.添加和修改相关方法n
		//5.1.点击添加弹出对话框
		handleAdd() {
      this.saveForm = {
        id: null,
        username:'',
        phone:'',
        email:'',
        password:'',
        age:'',
        state:1,
        logininfo_id:'',
        roles:[]
      }
      this.fileList = [];
      this.saveFormVisible = true;
      this.getRoles()
		},
		//5.2.点击修改弹出对话框
		handleEdit(row) {
      this.saveFormVisible = true;
      this.saveForm = Object.assign({},row);
      this.getRoles()
		},
		//5.3.提交表单数据操作数据库
		saveSubmit() {
      this.saveFormVisible = true;
      this.$refs.saveForm.validate((valid) => {
        if (valid){
          this.$http.put("/employee",this.saveForm).then(res => {
            if (res.data.success){
              this.$message({
                type: 'success',
                message: '添加成功!-200'
              });
              this.getTableData();
            } else {
              this.$message({
                type: 'error',
                message: '添加失败-500'
              });
            }
          }).catch(res => {
            this.$message({
              type: 'error',
              message: '网络繁忙,请稍后重试'
            });
          })
          this.saveFormVisible = false;
        }else {
          this.$message({
            type: 'error',
            message: '请检查必填项输入是否完整!'
          })
        }
      })
		},
    getRoles(){
      this.$http.get("/role").then(res => {
        let{success,data} = res.data
        if (success){
          this.options = data
        }
      })
    }
	},
	mounted() {
		this.getTableData();
	}
}

</script>

<style scoped></style>
